//
//  IconPlusView.swift
//  SCA
//
//  Created by tjun on 2025/9/7.
//

import UIKit

//class IconPlusView: UIView {
//    
//    private let stackView = UIStackView()
//    private var iconSize: CGFloat = 40
//    private var plusSize: CGFloat = 14
//    private var spacing: CGFloat = 6
//
//    override init(frame: CGRect) {
//        super.init(frame: frame)
//        setupView()
//    }
//
//    required init?(coder: NSCoder) {
//        super.init(coder: coder)
//        setupView()
//    }
//
//    private func setupView() {
//        stackView.axis = .horizontal
//        stackView.spacing = spacing
//        stackView.alignment = .center
//        stackView.translatesAutoresizingMaskIntoConstraints = false
//        addSubview(stackView)
//        
//        NSLayoutConstraint.activate([
//            stackView.topAnchor.constraint(equalTo: topAnchor),
//            stackView.bottomAnchor.constraint(equalTo: bottomAnchor),
//            stackView.centerXAnchor.constraint(equalTo: centerXAnchor)
//        ])
//    }
//
//    func configure(with icons: [UIImage]) {
//        stackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
//        
//        for (index, icon) in icons.enumerated() {
//
//            let iconView = UIImageView(image: icon)
//            iconView.contentMode = .scaleAspectFit
//            iconView.widthAnchor.constraint(equalToConstant: iconSize).isActive = true
//            iconView.heightAnchor.constraint(equalToConstant: iconSize).isActive = true
//            stackView.addArrangedSubview(iconView)
//            
//            if index < icons.count - 1 {
//                let plusLabel = UILabel()
//                plusLabel.text = "+"
//                plusLabel.textColor = .white
//                plusLabel.textAlignment = .center
//                plusLabel.font = .systemFont(ofSize: 20)
//                plusLabel.widthAnchor.constraint(equalToConstant: plusSize).isActive = true
//                plusLabel.heightAnchor.constraint(equalToConstant: plusSize).isActive = true
//                stackView.addArrangedSubview(plusLabel)
//            }
//        }
//    }
//
//    func calculateTotalWidth(iconCount: Int) -> CGFloat {
//        guard iconCount > 0 else { return 0 }
//        return CGFloat(iconCount) * iconSize + CGFloat(iconCount - 1) * plusSize + CGFloat(iconCount - 1) * spacing
//    }
//    
//    class func calculateTotalWidth(iconCount: Int, iconSize: CGFloat, plusSize: CGFloat, spacing: CGFloat) -> CGFloat {
//        guard iconCount > 0 else { return 0 }
//        return CGFloat(iconCount) * iconSize + CGFloat(iconCount - 1) * plusSize + CGFloat(iconCount - 1) * spacing
//    }
//}
//


class IconPlusView: UIView {
    private var iconViews: [UIImageView] = []
    private var plusLabels: [UILabel] = []
    
    private var iconSize: CGFloat = 40
    private var plusSize: CGFloat = 14
    private var spacing: CGFloat = 6
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupInitialSubviews(maxIcons: 6)
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupInitialSubviews(maxIcons: 6)
    }
    
    private func setupInitialSubviews(maxIcons: Int) {
        for _ in 0..<maxIcons {
            let iconView = UIImageView()
            iconView.contentMode = .scaleAspectFit
            addSubview(iconView)
            iconViews.append(iconView)
        }
        
        for _ in 0..<maxIcons - 1 {
            let plusLabel = UILabel()
            plusLabel.text = "+"
            plusLabel.textColor = .white
            plusLabel.textAlignment = .center
            plusLabel.font = .systemFont(ofSize: 20)
            addSubview(plusLabel)
            plusLabels.append(plusLabel)
        }
    }
    
    func configure(with icons: [String]) {
        let iconCount = icons.count
        
        for (i, iconView) in iconViews.enumerated() {
            if i < iconCount {
                iconView.setImage(url: icons[i])
                iconView.isHidden = false
            } else {
                iconView.isHidden = true
            }
        }
        
        for (i, plus) in plusLabels.enumerated() {
            plus.isHidden = i >= iconCount - 1
        }
        
        layoutSubviews() // 触发布局
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        var x: CGFloat = 0
        let visibleIcons = iconViews.filter { !$0.isHidden }
        let iconCount = visibleIcons.count
        
        for i in 0..<iconCount {
            let iconView = iconViews[i]
            iconView.frame = CGRect(x: x, y: 0, width: iconSize, height: iconSize)
            x += iconSize
            
            if i < iconCount - 1 {
                let plusLabel = plusLabels[i]
                plusLabel.frame = CGRect(x: x + (spacing / 2), y: (iconSize - plusSize) / 2, width: plusSize, height: plusSize)
                plusLabel.isHidden = false
                x += plusSize + spacing
            }
        }
    }
    
    class func calculateTotalWidth(iconCount: Int, iconSize: CGFloat = 40, plusSize: CGFloat = 14, spacing: CGFloat = 6) -> CGFloat {
        guard iconCount > 0 else { return 0 }
        return CGFloat(iconCount) * iconSize + CGFloat(iconCount - 1) * plusSize + CGFloat(iconCount - 1) * spacing
    }
}

 
import UIKit
import SnapKit

 
class PowerRowView: UIView {
    
    let titleLabel = UILabel(font: UIFontMediumMake(12), textColor: UIColor.white)
    let infoButton = UIButton(type: .system)
    private let valueLabel = UILabel(font: UIFontMediumMake(14), textColor: UIColor.white)
    var value: String? {
        didSet {
            valueLabel.text = value
        }
    }
    
    var model: PowerModel? {
        didSet {
            titleLabel.text = model?.title
            valueLabel.text = model?.totalDisp
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        titleLabel.addTo(self)
        titleLabel.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        
        infoButton.setImage(UIImage(named: "more-info-icon"), for: .normal)
        infoButton.addTo(self)
        infoButton.snp.makeConstraints { make in
            make.width.height.equalTo(20)
            make.left.equalTo(titleLabel.snp.right).offset(10)
            make.centerY.equalToSuperview()
        }
 
        valueLabel.addTo(self)
        valueLabel.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.centerY.equalToSuperview()
        }
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    

 
}

 
